package com.hh.core.exception;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.naming.NoPermissionException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.hh.admin.exception.LoginErrorException;
import com.hh.admin.view.ErrorView;
import com.hh.core.entity.ResultEntity;
import com.hh.core.enums.ExceptionEnum;
import com.hh.core.util.BaseUtil;
import com.hh.core.util.HttpCoreUtil;

/**
 * 
 * @ClassName: GlobalExceptionHandler
 * @Description: TODO(业务异常处理类)
 * @author hxn
 * @date 2018年1月23日 上午10:29:46
 *
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	private static final Log log = LogFactory.get();

	/**
	 * 
	 * @Title: viewException
	 * @Description: 页面类 异常捕获
	 * @param @param viewException
	 * @param @return 设定文件
	 * @return ModelAndView 返回类型
	 * @author hxn
	 * @date 2018年1月24日 上午10:46:31
	 * @throws
	 */
	@ExceptionHandler(ViewException.class)
	public ModelAndView viewException(ViewException viewException,
			HttpServletRequest request) {
		ExceptionEnum exceptionEnum = viewException.getExceptionEnum();
		ModelAndView mv = new ModelAndView(exceptionEnum.getUrlPath());
		mv.addObject("errorMsg", exceptionEnum.getMessage());
		mv.addObject("errorCode", exceptionEnum.getCode());
		log.info("请求错误:" + exceptionEnum.getMessage());
		if (viewException instanceof LoginErrorException) {
			
		}
		return mv;
	}

	/**
	 * 
	 * @Title: validationException
	 * @Description: 校验类 异常 捕获
	 * @param @param validationException
	 * @param @return 设定文件
	 * @return ModelAndView 返回类型
	 * @author hxn
	 * @date 2018年1月24日 上午10:46:50
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	@ExceptionHandler(ValidationException.class)
	public ModelAndView validationException(
			ValidationException validationException) {
		ModelAndView mv = new ModelAndView(new FastJsonJsonView());
		ResultEntity re = new ResultEntity();
		re.setStatus(false);
		if (validationException instanceof ConstraintViolationException) {
			List<String> errorList = new ArrayList<>();
			Set<ConstraintViolation<?>> constraintViolations = ((ConstraintViolationException) validationException)
					.getConstraintViolations();
			for (ConstraintViolation item : constraintViolations) {
				errorList.add(item.getMessage());
				log.info("请求错误:" + item.getMessage());
			}
			re.setData(errorList);
			// mv.addObject("errorMsg", errorList);
		}
		re.setMessage("请求参数校验失败");
		// mv.addObject("status", false);
		mv = BaseUtil.resultMv(mv, re);
		return mv;
	}

	/**
	 * 
	 * @Title: jsonException
	 * @Description: 返回json 异常信息
	 * @param @param jsonException
	 * @param @return 设定文件
	 * @return ResultEntity 返回类型
	 * @author hxn
	 * @date 2018年1月30日 下午3:00:38
	 * @throws
	 */
	@ExceptionHandler(JsonException.class)
	@ResponseBody
	public ResultEntity jsonException(JsonException jsonException) {
		ExceptionEnum exceptionEnum = jsonException.getExceptionEnum();
		ResultEntity re = new ResultEntity(false, exceptionEnum.getMessage(),
				jsonException.getData());
		return re;
	}

	/**
	 * 
	 * @Title: noPermissionException
	 * @Description: 权限不足
	 * @param @param request
	 * @param @return 设定文件
	 * @return ModelAndView 返回类型
	 * @author hxn
	 * @date 2018年2月8日 下午2:47:53
	 * @throws
	 */
	@ExceptionHandler(NoPermissionException.class)
	public ModelAndView noPermissionException(HttpServletRequest request) {
		ModelAndView mv = null;
		if (!HttpCoreUtil.assertAjax(request)) {// 不是ajax请求
			mv = new ModelAndView(ErrorView.ERROR_NOPERMISSION);// 权限不足
		} else {
			mv = new ModelAndView(new FastJsonJsonView());
			ResultEntity re = new ResultEntity(false, "访问失败", "权限不足");
			mv = BaseUtil.resultMv(mv, re);
		}
		return mv;
	}

	/**
	 * 
	 * @Title: exception
	 * @Description: 服务异常错误
	 * @param @param request
	 * @param @return 设定文件
	 * @return ModelAndView 返回类型
	 * @author hxn
	 * @date 2018年2月8日 下午2:59:08
	 * @throws
	 */
	@ExceptionHandler(value = { Exception.class, RuntimeException.class })
	public ModelAndView exception(HttpServletRequest request) {
		ModelAndView mv = null;
		if (!HttpCoreUtil.assertAjax(request)) { // 不是ajax请求
			mv = new ModelAndView(ErrorView.ERROR_500);
		} else {
			mv = new ModelAndView(new FastJsonJsonView());
			ResultEntity re = new ResultEntity(false, "访问失败", "服务器异常");
			mv = BaseUtil.resultMv(mv, re);
		}
		return mv;
	}

}
